package modelo;

import java.util.ArrayList;
import estructural.Artista;
import estructural.Audio;
import estructural.Formato;
import estructural.Genero;
import estructural.Lenguaje;
import estructural.Video;

public class InsertIntoH2 {
	private ConexionH2 conH2;

	public InsertIntoH2() {

	}

	public void insert(ArrayList<Artista> artL) {
		conH2 = new ConexionH2();
		
		for (Artista art : artL) {
			insertArtist(art);
			insertLanguage(art.getCancion().getLenguaje());
			insertGenre(art.getCancion().getGenero());
			insertFormat(art.getCancion().getVideo().getFormato());
			insertFormat(art.getCancion().getAudio().getFormato());
			insertVideo(art.getCancion().getVideo());
			insertAudio(art.getCancion().getAudio());
			insertSong(art);
		}
		
		conH2.closeConnection();
	};

	private void insertArtist(Artista art) {
		String cad = 	"INSERT INTO ARTISTA (ID_ART, NOM_ART) " +
						"VALUES (SEQ_ID_ART.NEXTVAL,'"+ art.getNomArt().toLowerCase() +"')";
		conH2.executeUpdateStatement(cad);
	}
	
	private void insertLanguage(Lenguaje len) {
		String cad = 	"INSERT INTO LENGUAJE (ID_LENG, TIP_LENG) " +
						"VALUES (SEQ_ID_LEN.NEXTVAL, '"+ len.getTipLeng().toLowerCase() +"')";
		conH2.executeUpdateStatement(cad);
	}
	
	private void insertGenre(Genero gen) {
		String cad = 	"INSERT INTO GENERO (ID_GEN, NOM_GEN) " +
						"VALUES (SEQ_ID_GEN.NEXTVAL, '"+ gen.getNomGen().toLowerCase() +"')";
		conH2.executeUpdateStatement(cad);
	}
	
	private void insertFormat(Formato form) {
		String cad = 	"INSERT INTO FORMATO (ID_FOR, TIPO_FOR) " +
						"VALUES (SEQ_ID_FOR.NEXTVAL,'"+ form.getTipoFor().toLowerCase() +"')";
		conH2.executeUpdateStatement(cad);
	}
	
	private void insertVideo(Video vid) {
		String cad = 	"INSERT INTO VIDEO (ID_VID, ID_FOR, TIT_VID, PATH_VID) " +
						"VALUES (SEQ_ID_VID.NEXTVAL, " +
								"SELECT ID_FOR FROM FORMATO WHERE TIPO_FOR = '"+ vid.getFormato().getTipoFor().toLowerCase() + "', '"
								+ vid.getTitVid().toLowerCase() + "', '" 
								+ vid.getPathVid() +"')";
		conH2.executeUpdateStatement(cad);
	}
	
	private void insertAudio(Audio aud) {
		String cad = 	"INSERT INTO AUDIO (ID_AUD, ID_FOR, TIT_AUD, PATH_AUD) " +
						"VALUES (SEQ_ID_AUD.NEXTVAL, " +
								"SELECT ID_FOR FROM FORMATO WHERE TIPO_FOR = '"+ aud.getFormato().getTipoFor().toLowerCase() + "', '"
								+ aud.getTitAud().toLowerCase() + "', '" 
								+ aud.getPathAud() +"')";
		conH2.executeUpdateStatement(cad);	
	}
	
	private void insertSong(Artista art) {
		String cad = 	"INSERT INTO CANCION (ID_CAN, ID_LENG, ID_GEN, ID_ART, ID_AUD, ID_VID, TIT_CAN) " +
						"VALUES (SEQ_ID_AUD.NEXTVAL, " +
								"SELECT ID_LENG FROM LENGUAJE WHERE TIP_LENG = '"+ art.getCancion().getLenguaje().getTipLeng().toLowerCase() + "', "+
								"SELECT ID_GEN FROM GENERO WHERE NOM_GEN = '"+ art.getCancion().getGenero().getNomGen().toLowerCase() + "', "+
								"SELECT ID_ART FROM ARTISTA WHERE NOM_ART = '"+ art.getNomArt().toLowerCase() + "', "+
								"SELECT ID_AUD FROM AUDIO WHERE PATH_AUD = '"+ art.getCancion().getAudio().getPathAud() + "', "+
								"SELECT ID_VID FROM VIDEO WHERE PATH_VID = '"+art.getCancion().getVideo().getPathVid() + "', '"+
								art.getCancion().getTitCan().toLowerCase() +"')";
		conH2.executeUpdateStatement(cad);
	}
	
}
